package com.xwh.shop.goods.service.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwh.shop.common.Result;
import com.xwh.shop.goods.model.entity.Category;
import com.xwh.shop.goods.model.vo.CategoryListVO;
import com.xwh.shop.goods.service.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwh
 * @since 2025-07-16
 */
@Slf4j
@RestController
@RequestMapping("/category")
@Tag(name = "商品分类管理")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @Operation(summary = "添加分类")
    @GetMapping("")
    public Result<Object> add(@RequestBody Category category){
        log.info("添加分类 category:{}",category);
        categoryService.save(category);
        return Result.success();
    }


    @Operation(summary = "通过Id查询")
    @GetMapping("/{id}")
    public Result<Object> findById(@PathVariable Long id){
        Category category = categoryService.getById(id);
        log.info("找到的分类 category:{}",category);
        return Result.success(category);
    }


    @Operation(summary = "删除分类")
    @DeleteMapping("{id}")
    public Result<Boolean> delete(@PathVariable Long id){
        log.info("删除分类 id:{}",id);
        boolean res = categoryService.removeById(id);
        return Result.success(res);

    }

    @Operation(summary = "修改分类")
    @PutMapping("")
    public Result<Boolean> update(@RequestBody Category category){
        log.info("修改分类 category:{}",category);
        boolean res = categoryService.updateById(category);
        return Result.success(res);

    }

    @Operation(summary = "通过名称查询分类")
    @GetMapping("/page")
    public Result<Page<Category>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Byte isShow){
        log.info("分页查询商品分类列表：current={}, pageSize={}, name={}, status={}", current, pageSize, name, isShow);
        Page<Category> categoryPage = categoryService.pageQuery(current, pageSize, name, isShow);
        log.info("categoryPage:{}",categoryPage);
        return Result.success(categoryPage);
    }

    @Operation(summary = "查询所有分类")
    public Result<List<CategoryListVO>> list(){
        List<CategoryListVO> pageVOList = categoryService.getList();
        return Result.success(pageVOList);
    }

}
